<?php
	// check params size and names
	if (sizeof($_GET) != 3 || !array_key_exists('hdi', $_GET) || !array_key_exists('appkey', $_GET) || !array_key_exists('stopped', $_GET)) 
	{
		echo "Usage";
		exit;
	}

	// if passed then connect to db and define vars
	$mysqli = new mysqli('localhost', 'root', 'project3', 'implauth');
	if ($mysqli->connect_errno)
	{
		echo "db connection error!";
		exit;
	}
	$hdi = $_GET['hdi'];
	$appkey = $_GET['appkey'];
	$stopped = $_GET['stopped'];

	// if passed then validate hdi and appkey
	$res = $mysqli->query("select count(*) as total from hdvid_appkey where hdvid like '$hdi' and appkey like '$appkey'");
	$ob = $res->fetch_object();
	$res->close();
	if ($ob->total != 1)
	{
		echo "appkey/deviceid mismatch!";
		$mysqli->close();
		exit;
	}

	// if passed then update record
	$res = $mysqli->query("update locations set stopped = '$stopped' where hdeviceid like '$hdi'");
	if (!$res)
	{
		echo "update failed!";
		$mysqli->close();
		exit();
	}

	// if passed then echo 0
	echo 0;
	$mysqli->close();
?>
